var app = getApp()
Page({
  data: {
    data_url: "",
    address: [],
    forBuy: false,
  },
  jumpNewaddress: function (e) {
    wx.navigateTo({
      url: '/pages/newaddress/newaddress',
    })
  },
  change_sel: function (e) {
    var that = this
    var address = that.data.address
    wx.showLoading({
      title: '加载中',
    })
    app.getSessionId().then(function () {
      wx.request({
        url: that.data.data_url + "/GPD/setDefaultAddress.do",
        data: {
          session_id: app.globalData.sessionId,
          address_id: e.currentTarget.dataset.id
        },
        success: function (res) {
          address.forEach(function (item) {
            wx.hideLoading()
            if (item.id == e.currentTarget.dataset.id) {
              item.isDefault = 1
            } else {
              item.isDefault = 0
            }
          })
          if (res.data.code == 0) {
            if (that.data.forBuy) {
              wx.navigateBack({
                delta: 1
              })
            }
            that.setData({
              address: address
            })
          }
        }
      })
    })
  },
  modifyAdd: function (e) {
    var that = this
    wx.navigateTo({
      url: '/pages/newaddress/newaddress?id=' + e.currentTarget.dataset.id
    })
  },
  delAdd: function (e) {
    var that = this
    wx.showModal({
      title: '温馨提示',
      content: '是否要删除本地址？',
      success: function (res) {
        if (res.confirm) {
          var id = e.currentTarget.dataset.id
          app.getSessionId().then(function () {
            wx.request({
              url: that.data.data_url + "/GPD/removeAddress.do",
              data: {
                address_id: id,
                session_id: app.globalData.sessionId
              },
              success: function (res) {
                if (res.data.code == 0) {
                  var arr2 = that.data.address
                  arr2.splice(e.currentTarget.dataset.index, 1)
                  that.setData({
                    address: arr2
                  })
                } else {
                  wx.showToast({
                    title: res.data.message,
                    image: '/images/close.png'
                  })
                }
              }
            })
          })
        }
      }
    })

  },
  onLoad: function (options) {

    this.setData({
      data_url: app.globalData.data_url,
      forBuy: options.forBuy || false
    })
  },
  onReady: function () {

  },
  onShow: function () {
    var that = this
    app.getSessionId().then(function () {
      wx.request({
        url: that.data.data_url + "/GPD/getAllAddress.do",
        data: {
          session_id: app.globalData.sessionId
        },
        success: function (res) {
          that.setData({
            address: res.data.ras
          })
        }
      })
    })
  },
  onHide: function () {

  },
  onUnload: function () {

  },
  onPullDownRefresh: function () {

  },
  onReachBottom: function () {

  }
})		